package com.hfh.web;

import com.hfh.management.comments.CommentsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yzYang on 14-2-18.
 */
@Controller
public class CommentsViewController {

    @Autowired
    private CommentsService commentsService;

    private int pageLimit =10;

    private final int DEFAULT_PAGE_SIZE = 10;

    @RequestMapping(value="/ccomments",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getcommentList(@RequestParam(value="p",required = false) String id,
                            @RequestParam(value="showDetail",required = false) String isShowDetail,
                            @RequestParam(value="startNo",required = false) String startNo,
                            @RequestParam(value="pagesize",required = false) int pagesize){

    	startNo = StringUtils.isBlank(startNo)?"0":startNo;
        pagesize =pagesize==0?DEFAULT_PAGE_SIZE:pagesize;

        Map<String,Object> cComments = commentsService.getCommentsList(startNo,pagesize);
        return cComments;
    }

    @RequestMapping(value="/commitComments",method = RequestMethod.POST)
//    @ResponseBody

    public String commitComments(@RequestParam(value="content",required = false) String ccontent,
                                             @RequestParam(value="cuName",required = false) String name )
    {
        boolean r = commentsService.saveComment(ccontent,name);
        Map<String,String> m = new HashMap<String, String>();
        if(r){
            m.put("result","success");
        }else{
            m.put("result","failed");
        }
        return "redirect:commetsView";
    }
//    public Map<String,String> commitComments(@RequestParam(value="content",required = false) String ccontent,
//                                      @RequestParam(value="cuName",required = false) String name )
//    {
//        boolean r = commentsService.saveComment(ccontent,name);
//        Map<String,String> m = new HashMap<String, String>();
//        if(r){
//            m.put("result","success");
//        }else{
//            m.put("result","failed");
//        }
//        return m;
//    }
}
